var address = "172.20.41.71:7071";
var token = layui.sessionData("userToken").key;

//下面ws/ 后面是用户token
var url = "ws://" + address + "/ws/" + token.split(" ")[1];
//判断浏览器是否支持websocket
if ("WebSocket" in window) {
  //如果支持，创建websocket对象，注意url格式
  websocket = new ReconnectingWebSocket(url);
  websocket.debug = true;
  //websocket.timeoutInterval=2000;重连的最长等待时间
} else {
  alert("浏览器版本不支持websocket！");
}
//定义连接错误的回调方法
websocket.onerror = function () {
  console.log("websocket连接出错！");
};
//定义连接成功的回调方法
websocket.onopen = function () {
  console.log("websocket连接成功！");
};
//定义websocket关闭的回调方法
websocket.onclose = function () {
  console.log("websocket已关闭！");
};

//当窗口关闭时，主动去关闭websocket连接
window.onbeforeunload = function () {
  closeWebSocket && closeWebSocket();
};

layui.sessionData("noticeMeetingLength", {
  key: "key",
  value: 0,
});
//接收到消息的回调方法
websocket.onmessage = function (event) {
  noticeMeetingLengthFun();
  var arr = JSON.parse(event.data);
  var newData = arr["data"];
  if (arr["type"] == "message") {
    newData.forEach((element) => {
      indexLength = layer.open({
        type: 1,
        closeBtn: 0,
        skin: "layerClass",
        offset: "t",
        area: ["500px", "240px"],
        btn: ["确定"],
        title: "会议信息",
        content:
          '<div class="messageBox">' +
          '<div class="messageIcon"><i class="layui-icon layui-icon-dialogue"></i></div>' +
          "<div data-id=" +
          element.id +
          ">" +
          element.content +
          "</div></div>",
        shade: 0, //不显示遮罩
        success: function (layero, index) {},
        yes: function (index, layero) {
          $.ajax({
            type: "post",
            async: true, //异步执行
            url: base_url + "/messages/updateMessage",
            headers: {
              Accept: "application/json; charset=utf-8",
              Authorization: layui.sessionData("userToken").key,
            },
            data: { id: element.id, readTime: 1 },
            success: function (data) {
              if (data.type == "success") {
                noticeMeetingLengthFun();
                layer.close(index);
              }
            },
          });
        },
        cancel: function (index, layero) {},
      });
    });
  } else {
    layer.open({
      type: 1,
      closeBtn: 0,
      skin: "layerClass",
      area: ["500px", "240px"],
      btn: ["确定"],
      title: "会议通知",
      content:
        '<div class="messageBox">' +
        '<div class="messageIcon"><i class="layui-icon layui-icon-dialogue"></i></div>' +
        "<div>" +
        arr["data"] +
        "</div></div>",
      shade: 0, //不显示遮罩
      success: function (layero, index) {},
      yes: function (index, layero) {
        noticeMeetingLengthFun();
        layer.close(index);
      },
      cancel: function (index, layero) {},
    });
  }
};

function noticeMeetingLengthFun() {
  $.ajax({
    type: "GET",
    async: true, //异步执行
    url: base_url + "/messages/unReads",
    headers: {
      Accept: "application/json; charset=utf-8",
      Authorization: layui.sessionData("userToken").key,
    },
    success: function (data) {
      if (data.type == "success") {
        layui.sessionData("noticeMeetingLength", {
          key: "key",
          value: data.data,
        });
        $(".layadmin-iframe")
          .contents()
          .find(".noticeMeetingTotal")
          .text(data.data);

        // var iframeLength = $(window.parent.document).find("iframe");
        // for (var i = 0; i < iframeLength.length; i++) {
        //   $(window.parent.document)
        //     .find("iframe")
        //     [i].contentWindow.location.reload(true);
        // }
      }
    },
  });
}
